
//
//  WBNewFeatureView.swift
//  SinaWebo
//
//  Created by 亚杏 on 2017/5/22.
//  Copyright © 2017年 oms. All rights reserved.
//

import UIKit

//新特性视图     
class WBNewFeatureView: UIView,UIScrollViewDelegate {

    @IBOutlet weak var newScrollView: UIScrollView!
    @IBOutlet weak var enterBtn: UIButton!
    @IBOutlet weak var pageNum: UIPageControl!


    //进入微博
    @IBAction func EnterWB(_ sender: Any) {

        removeFromSuperview()
    }

    class func newFeatureView() -> WBNewFeatureView {
        let nib = UINib(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        //从XIB加载视图是默认为600*600的
        v.frame = UIScreen.main.bounds
        return v

    }

    override func awakeFromNib() {
        //如果使用自动布局设置的界面，从xib加载，默认是600*600大小
        //添加四个图像视图
        let count = 4
        let rect = UIScreen.main.bounds
        for i in 0..<count {
            let imageName = "new_feature_\(i+1)"
            let iv = UIImageView(image: UIImage(named:imageName))
            //设置图像大小
            iv.frame = rect.offsetBy(dx: CGFloat(i)*rect.width, dy: 0)
            newScrollView.addSubview(iv)
        }

        //指定scrollView属性
        newScrollView.contentSize = CGSize(width: CGFloat(count + 1)*rect.width, height: rect.height)
        newScrollView.bounces = false
        newScrollView.isPagingEnabled = true
        newScrollView.showsVerticalScrollIndicator = false
        newScrollView.showsHorizontalScrollIndicator = false
        newScrollView.delegate = self
        // 隐藏按钮
        enterBtn.isHidden = true
    }
    
    //MARK: UIScrollViewDelegate
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //1.滚动到最后一屏,删除视图
        let page = Int(scrollView.contentOffset.x/scrollView.bounds.width)
        //2.判断是否是最后一页
        if page == scrollView.subviews.count {
            removeFromSuperview()
        }
        //3.如果是倒数第二页,显示按钮
        enterBtn.isHidden = (page != scrollView.subviews.count - 1)

//        if page == scrollView.subviews.count - 1 {
//            enterBtn.isHidden = false
//        }
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //一滚动就隐藏按钮
        enterBtn.isHidden = true
        //计算当前的偏移量
        let page = Int(scrollView.contentOffset.x/scrollView.bounds.width + 0.5)
        //设置分页控件 
        pageNum.currentPage = page
        //分页控件隐藏
        pageNum.isHidden = (page == scrollView.subviews.count)
    }


}
